package istar.impl;

import istar.IActor;
import istar.IElement;
import istar.IElementLink;
import istar.ITask;

import java.util.Iterator;
import java.util.Vector;


public class TaskDecompositionLink extends AbstractElementLink implements IElementLink{

	//public static final int OPEN = 0;
	//public static final int COMMITTED = 1;
	
	private int type=0;
	

	public TaskDecompositionLink(ITask ownerTask, String name, IElement _subElement) {
		super(name, ownerTask, _subElement);
		ownerTask.setMyDecompositionLink(this);
		_subElement.getSupports().add(this);
	} 
	
	
	public int getType() {
		return type;
	}

	public void setType(int value) {
		this.type = value;
	}


	@Override
	public void setOwner(IElement elmnt) throws Exception {
		try {
			super.setOwner((ITask)elmnt);
		} catch (Exception e) {
			throw e;
		}		
	}


	public Object clone(IActor newOwner) throws CloneNotSupportedException {
		TaskDecompositionLink newClone = (TaskDecompositionLink)super.clone();
		newClone.setName(this.getName());
		

		newClone.setType(this.getType());
		newClone.setTargetElements(new Vector<IElement>());

		Iterator<IElement> itSubElmnts = this.getTargetElements().iterator();
		
		while (itSubElmnts.hasNext()){
		
			AbstractElement subElmnt = (AbstractElement)itSubElmnts.next();
			if (subElmnt.getOwner()==this.getOwner().getOwner()){
				AbstractElement subClone = (AbstractElement)subElmnt.clone(newOwner);
				newClone.getTargetElements().add(subClone);
				subClone.getSupports().add(newClone);
			}
			else {
				AbstractElement subClone = (AbstractElement)subElmnt.clone(null);
				newClone.getTargetElements().add(subClone);
			}
		}
		
		return newClone;
	}

	
}
